<template>
  <Panel>
    <template #title>商品添加</template>
    <template #content>
      <el-form
        ref="ruleForm"
        :model="addform"
        label-width="80px"
        size="mini"
        :rules="rules"
      >
        <el-form-item label="商品名称" size="mini" prop="name">
          <el-input v-model="addform.name" placeholder="商品名称"></el-input>
        </el-form-item>
        <el-form-item label="商品分类" size="mini" prop="category">
          <el-select v-model="addform.category" placeholder="请选择商品分类">
            <el-option
              v-for="item in categories"
              :key="item.cateName"
              :label="item.cateName"
              :value="item.cateName"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商品价格" size="mini">
          <el-input-number
            v-model="addform.price"
            :min="1"
            :max="9999"
          ></el-input-number>
        </el-form-item>
        <!-- 商品图片 -->
        <el-form-item label="商品图片">
          <el-upload
            class="avatar-uploader"
            action="http://127.0.0.1:5000/goods/goods_img_upload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="imageUrl" :src="imageUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <!-- 表单域 -->
        <el-form-item label="商品描述" size="mini" prop="goodsDesc">
          <el-input type="textarea" v-model="addform.goodsDesc"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button v-waves type="primary" @click="submitForm('ruleForm')"
            >立即创建</el-button
          >
          <el-button>取消</el-button>
        </el-form-item>
      </el-form>
    </template>
  </Panel>
</template>

<script>
import { allCate, addGoods } from '@/api/good.js'
import { success, error } from '@/utils/hint'
export default {
  data() {
    return {
      imageUrl: '',
      categories: [],
      addform: {
        name: '',
        category: '',
        price: 1,
        goodsDesc: '',
        imgUrl: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        category: [
          { required: true, message: '请选择商品分类', trigger: 'change' }
        ],
        goodsDesc: [
          { required: true, message: '请填写活动形式', trigger: 'blur' }
        ]
      }
    }
  },

  created() {
    this.getAllCate()
  },
  mounted() {},

  methods: {
    async getAllCate() {
      const { categories } = await allCate()
      this.categories = categories
    },
    handleChange(value) {},
    async submitForm(formName) {
      this.$refs[formName].validate(async valid => {
        if (valid) {
          /* 判断是否选择图片 */
          if (this.imageUrl) {
            const { code } = await addGoods(this.addform)
            if (code === 0) {
              setTimeout(() => this.$router.push('/goods/list'), 1500)
            }
          } else {
            error('请上传图片')
          }
        }
      })
    },
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw)
      if (res.code === 0) {
        success(res.msg)
        this.addform.imgUrl = res.imgUrl
      }
    },
    beforeAvatarUpload(file) {
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isLt2M
    }
  }
}
</script>

<style lang="less" scoped>
.el-input {
  width: 200px;
}
/deep/textarea {
  width: 300px;
  height: 80px;
}
/deep/.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 100px;
  height: 100px;
  line-height: 100px;
  text-align: center;
}
.avatar {
  width: 100px;
  height: 100px;
  display: block;
}
</style>
